*
* Table 4: The aggregate effect of dominated choices
*
* Input table: 
*	"${data_applicant_level_tidy}"
*
* Output file: 
*	/results/Table4.tex
*

use "${data_applicant_level_tidy}", clear

cap file close sumstat
file open sumstat using "${result_dir}/Table4.tex", write replace
file write sumstat "\begin{tabular}{l c c c} \\ \hline\hline" _n
file write sumstat " & \multicolumn{1}{c}{Benchmark} & \multicolumn{1}{c}{State-funded above} & \multicolumn{1}{c}{State-funded}  \\ " _n
file write sumstat " &  & \multicolumn{1}{c}{self-funded} & \multicolumn{1}{c}{on top}  \\ " _n
file write sumstat " & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)}  \\ \hline " _n

* Panel A: Admitted applicants
file write sumstat "\multicolumn{4}{l}{A. {\it Admitted applicants}} \\ " _n

* Admitted applicants
gen admitted = contract_id != "None"
qui sum admitted
local nobs_benchmark: display %6.1f r(mean)*100

gen admitted_lower = contract_id_lower != "None"
qui sum admitted_lower
local nobs_lower: display %6.1f r(mean)*100

gen admitted_upper = contract_id_upper != "None"
qui sum admitted_upper
local nobs_upper: display %6.1f r(mean)*100

file write sumstat "\ - admitted applicants &  `nobs_benchmark' & `nobs_lower' & `nobs_upper' \\ " _n
drop admitted admitted_lower admitted_upper

* Admitted applicants with state-funding
gen admitted = contract_id != "None" & state_funded == 1
qui sum admitted
local nobs_benchmark: display %6.1f r(mean)*100
gen admitted_lower = contract_id_lower != "None" & state_funded_lower == 1
qui sum admitted_lower
local nobs_lower: display %6.1f r(mean)*100
gen admitted_upper = contract_id_upper != "None" & state_funded_upper == 1
qui sum admitted_upper
local nobs_upper: display %6.1f r(mean)*100

file write sumstat "\ - admitted applicants with state-funding &  `nobs_benchmark' & `nobs_lower' & `nobs_upper' \\ " _n
drop admitted admitted_lower admitted_upper

* Admitted applicants with self-funding
gen admitted = contract_id != "None" & state_funded == 0
qui sum admitted
local nobs_benchmark: display %6.1f r(mean)*100
gen admitted_lower = contract_id_lower != "None" & state_funded_lower == 0
qui sum admitted_lower
local nobs_lower: display %6.1f r(mean)*100
gen admitted_upper = contract_id_upper != "None" & state_funded_upper == 0
qui sum admitted_upper
local nobs_upper: display %6.1f r(mean)*100

file write sumstat "\ - admitted applicants with self-funding &  `nobs_benchmark' & `nobs_lower' & `nobs_upper' \\ " _n
drop admitted admitted_lower admitted_upper
file write sumstat " & & & \\ " _n


* Panel B: Winners

qui sum winner_lower
local nobs_lower: display %6.1f r(mean)*100
qui sum winner_upper
local nobs_upper: display %6.1f r(mean)*100

file write sumstat "B. {\it Winners} &  & `nobs_lower' & `nobs_upper' \\ " _n


* Winners -- newly assigned
gen winner_newly_assigned_lower = (winner_lower == 1 & rank_lower < 100 & rank == 100)
qui sum winner_newly_assigned_lower
local nobs_lower: display %6.1f r(mean)*100

gen winner_newly_assigned_upper = (winner_upper == 1 & rank_upper < 100 & rank == 100)
qui sum winner_newly_assigned_upper
local nobs_upper: display %6.1f r(mean)*100

file write sumstat "\ - newly admitted applicants &  & `nobs_lower' & `nobs_upper' \\ " _n
drop winner_newly_assigned_lower winner_newly_assigned_upper

* Winners -- same program
gen winner_same_program_lower = (winner_lower == 1 & rank_lower < 100 & rank < 100 & program_id_lower == program_id)
qui sum winner_same_program_lower
local nobs_lower: display %6.1f r(mean)*100
gen winner_same_program_upper = (winner_upper == 1  & rank_upper < 100 & rank < 100 & program_id_upper == program_id)
qui sum winner_same_program_upper
local nobs_upper: display %6.1f r(mean)*100

file write sumstat "\ - applicants admitted to the same program &  & `nobs_lower' & `nobs_upper' \\ " _n
drop winner_same_program_lower winner_same_program_upper

* Winners -- different program
gen winner_diff_program_lower = (winner_lower == 1 & rank_lower < 100 & rank < 100 & program_id_lower != program_id)
qui sum winner_diff_program_lower 
local nobs_lower: display %6.1f r(mean)*100
gen winner_diff_program_upper = (winner_upper == 1 & rank_upper < 100 & rank < 100 & program_id_upper != program_id)
qui sum winner_diff_program_upper
local nobs_upper: display %6.1f r(mean)*100

file write sumstat "\ - applicants admitted to a different program &  & `nobs_lower' & `nobs_upper' \\ " _n
drop winner_diff_program_lower winner_diff_program_upper
file write sumstat " & & & \\ " _n

* Panel C: Losers

* Losers
qui sum loser_lower
local nobs_lower: display %6.1f r(mean)*100
qui sum loser_upper
local nobs_upper: display %6.1f r(mean)*100

file write sumstat "C. {\it Losers} &  & `nobs_lower' & `nobs_upper' \\ " _n

* Losers -- newly unassigned
gen loser_newly_unassigned_lower = (loser_lower == 1 & rank_lower == 100 & rank < 100)
qui sum loser_newly_unassigned_lower
local nobs_lower: display %6.1f r(mean)*100
gen loser_newly_unassigned_upper = (loser_upper == 1 & rank_upper == 100 & rank < 100)
qui sum loser_newly_unassigned_upper
local nobs_upper: display %6.1f r(mean)*100

file write sumstat "\ - newly rejected applicants &  & `nobs_lower' & `nobs_upper' \\ " _n
drop loser_newly_unassigned_lower loser_newly_unassigned_upper

* Losers -- same program
gen loser_same_program_lower = (loser_lower == 1 & rank_lower < 100 & rank < 100 & program_id_lower == program_id)
qui sum loser_same_program_lower
local nobs_lower: display %6.1f r(mean)*100
gen loser_same_program_upper = (loser_upper == 1 & rank_upper < 100 & rank < 100 & program_id_upper == program_id)
qui sum loser_same_program_upper
local nobs_upper: display %6.1f r(mean)*100

file write sumstat "\ - applicants admitted to the same program &  & `nobs_lower' & `nobs_upper' \\ " _n
drop loser_same_program_lower loser_same_program_upper

* Losers -- different program
gen loser_diff_program_lower = (loser_lower == 1 & rank_lower < 100 & rank < 100 & program_id_lower != program_id)
qui sum loser_diff_program_lower
local nobs_lower: display %6.1f r(mean)*100
gen loser_diff_program_upper = (loser_upper == 1 & rank_upper < 100 & rank < 100 & program_id_upper != program_id)
qui sum loser_diff_program_upper
local nobs_upper: display %6.1f r(mean)*100

file write sumstat "\ - applicants admitted to a different program &  & `nobs_lower' & `nobs_upper' \\ \hline\hline " _n
drop loser_diff_program_lower loser_diff_program_upper

file write sumstat "\end{tabular}" _n
file close sumstat
